<?php

	// +----------------------------------------------------------------------
	// | 权限组控制器
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\controller\ucenter\admin;

	use app\controller\admin\Common;
	use app\event\ucenter\User as UserEvent;
	use yicmf\builder\Edit;
	use app\common\event\Job;
	use app\model\ucenter\UserGrade as UserGradeModel;
	use app\event\ucenter\UserGrade as UserGradeEvent;
	use yicmf\builder\Table;

	class UserGrade extends Common
	{
		protected function initialize()
		{
			parent::initialize();
		}

		public function _filter()
		{
			$where[] = ['status', '>=', 1];
			return $where;
		}


		public function index()
		{
			// 显示页面
			$builder = new Table();
			$builder->model(UserGradeModel::class)
				->where($this->_filter())
				->order('id DESC')
				->suggest('更新等级经验之后，需要手动刷新一下用户等级')
				->buttonUpdate('ucenter/admin/UserGrade/update')
				->buttonRefresh('refresh', '刷新用户等级')
				->keyId()
				->keyText('title', '分组名称')
				->keyText('name', '标识')
				->keyText('experience', '达成等级经验')
				->keyText('capping_experience', '封顶经验')
//                ->keyText('buyer_user', '购买人数')
//                ->keyText('register_user', '邀请人数')
//                ->keyText('order_count', '成交单数')
//                ->keyText('order_amount', '成交总额')
//                ->keyText('description', '简介')
				->keyCount('users', '用户量')
				->keyUpdateTime()
				->actionUpdate('update?id={$id}')
				->actionDelete('del?id={$id}');
			return $builder->fetch();
		}

		/**
		 * 更新新增用户组
		 */
		public function update()
		{
			if ($this->request->isPost()) {
				$result = $this->validate($this->request->post(), 'UserGrade');
				if (true === $result) {
					parent::_update();
				} else {
					$this->_returnError('数据安全验证失败，失败信息【' . $result . '】');
				}
			} else {
				//判断是否为编辑模式
				$isEdit = $this->request->has('id', 'get') ? true : false;
				if ($isEdit) {
					$data = UserGradeModel::find($this->request->get('id'));
				} else {
					$data = [];
					$data['status'] = 1;
					$data['type'] = $this->request->get('type');
				}


				$builder = new Edit();
				$builder->title($isEdit ? '编辑分组' : '添加分组')
					->keyHidden('id')
					->keyTextInline('title', '用户组中文名称', '用户组中文名称')
					->keyTextInline('name', '标识')
//                    ->keyNumber('buyer_user', '购买人数')
//                    ->keyNumber('register_user', '邀请人数')
//                    ->keyNumber('order_count', '成交单数')
//                    ->keyNumber('order_amount', '成交总额')
					->keyNumber('experience', '达成等级经验', '用户满足此分则为当前等级')
					->keyNumber('capping_experience', '封顶经验', '用户超过此分则晋级')
					->keyTextArea('description', '简介')
					->data($data)
					->buttonSubmit('', '保存')->buttonClose();
				return $builder->fetch();
			}
		}

		public function refresh()
		{
			Job::addTask('user_grade_reset', []);
			$this->_returnSuccess('刷新已经提交，请在队列任务内查看', false);
		}

	}
